$(function(){
	showCondition(false);
	
	//接收参数
	var param=$("#tabContent > .tab-pane.active").data();
    
	loadCase(param) ;
	
   //下一页加载
 	$("div[class='pagination'] li a[class='next']").click(function(){
 		$("#page").val(parseInt($("#page").val())+1);
 	});

 	//加载上一页
 	$(".previous.paginate_button").click(function(){
 		$("#page").val(parseInt($("#page").val())-1);
 	});
})

/**
 * 加载表格div
 * @param param
 */
function loadCase(param) {
	var tb_id='tb'+new Date().getTime();
	$('#'+tabId+' #content').html('<table class="table display nowrap" style="width:100%;"  id="'+tb_id+'"'
		 		+'style="text-align: center;">'     //transition:all .2s;
		 		+'</table>');
	loadPageData(tb_id,param);
}

/**
 * 加载表格data
 * @param tb_id
 * @param param
 * @param condition
 */
function loadPageData(tb_id,param,condition) {
	var columns=[
		             { "data": "DEPNAME",title:"科室"},
		             { 
		 				"data": "MR_NO",title:"病案号",
		 				"render": function(obj, type, full){
		 					var mrNo = full.MR_NO;
		 					return "<a href=\"javascript:medicalRecord('"+full.PMAININDEX+"','"+full.PATIENT_NAME+"')\">"+mrNo+"</a>";
		 				}
		             },
		             { "data": "PATIENT_NAME",title:"姓名" },
		             { "data": "SEX",title:"性别" },
		             { 
		           	  "data": "AGE_YEAR" ,title:"年龄",
		           	  "render": function(obj, type, full) {
		           		  if(full.AGE_YEAR>0){
		           			  return full.AGE_YEAR;
		           		  } else if(full.AGE_MONTH>0 && full.AGE_DAYS>0){
		           			return full.AGE_MONTH + '月' + full.AGE_DAYS + '天';
		           		  } else if(full.AGE_MONTH>0){
		           			  return full.AGE_MONTH + '月';
		           		  } else if(full.AGE_DAYS>0){
		           			  return full.AGE_DAYS + '天';
		           		  } 
		           	  }
		             },
		             { "data": "IN_DATE",title:"入院时间"},
		             { "data": "OUT_DATE",title:"出院时间",
		           	  "render": function(obj, type, full) {
		           		  return full.OUT_DATE;
		           	  }
		             },
		             { "data": "OPE_CODE" ,title:"手术编码"},
		             { "data": "OPE_DESC" ,title:"手术名称","render": function(obj, type, full) {
			            	 return linefeedText(obj);
			             }
		             },
		             { "data": "OPER_LEVEL" ,title:"术者级别"},
		             { "data": "OPER_DOCTOR" ,title:"术者"},
		             { "data": "ASSIS_ONE" ,title:"一助"},
		             { "data": "ASSIS_TWO" ,title:"二助"},
		             { "data": "HEALGRADE_DESC" ,title:"切口愈合等级"},
		             { "data": "ANESTHESIA" ,title:"麻醉方式"},
		             { "data": "ANESTHETIST" ,title:"麻醉医师"}
	             ];
	
	var ajaxFunction=function (aoData, fnCallback,oSetting ) {
		orderColumn=columns[aoData.order[0].column].data;//获取排序的字段
		orderMethod=aoData.order[0].dir;//获取asc desc
     	 var iDisplayStart=aoData.start;
     	 var iDisplayLength=aoData.length;
     	 var sEcho = 1;
     	 
     	 //获取病例总条数
     	 var caseTotalCount = parseInt($('#'+tabId+' #caseTotalCount').val());
     	 param.type2=condition;
      	 param.page=iDisplayStart; //开始行数
     	 param.pageSize=iDisplayLength;
     	 param.sEcho=sEcho;
     	 param.caseTotalCount=caseTotalCount;
     	 param.orderColumn=orderColumn;
     	 param.orderMethod=orderMethod;
     	 $.ajax( {
	             "dataType": 'json', 
	             "type": "POST", 
	             "url": contextPath + "/operationAnalysis/findOperationCaseDetail.dhc",
	             "data": param,
	             "success": function(resp) {
	            	var data=[];
	            	 if(resp.success){
	            		$('#'+tabId+' #caseTotalCount').val(resp.data.iTotalDisplayRecords);//总条数赋值到隐藏域
	            	 }
	            	 data.draw=aoData.draw;
	            	 // 总记录数
	            	 data.recordsTotal=resp.data.iTotalRecords;
	            	 // 过滤后的总记录数
	            	 data.recordsFiltered=resp.data.iTotalDisplayRecords;
	            	 data.data=resp.data.caseList;
					 fnCallback(data,oSetting);
	             }
        } );
      
     };
     var columnDefs = [{"orderable": true,"targets": [ 0,4,5,7,8,14 ] },{"orderable": false,"targets": '_all' }] ;
     var paramsDataTables={ajaxFunction:ajaxFunction,columnDefs:columnDefs,tableTitle:'手术病例明细表',scrollX:true,scrollY:'350px',autoWidth:false,orderCellsTop:true,pageLength:50,lengthMenu:[[ 50,100,500, 1000,99999],[ 50,100,500, 1000,'全部']]};
     var oTable=$('#'+tabId+' #'+tb_id).dataTableServerPaging(columns,paramsDataTables);
     $('#'+tabId+' #'+tb_id+"_wrapper .dataTables_scrollBody table tbody").attr('id','table');//tbody加上ID，病案号的样式
     $('#'+tabId+' #'+tb_id+"_wrapper .dataTables_scrollHeadInner table thead th").css({'text-align':'center'});
 	 oTable.columns.adjust();//tbody加了样式，重新调整宽度
 	if(condition){
		$("#"+tabId+" #"+tb_id+"_filter").html("<label>搜索 <input type='text' id='"+tb_id+"_filter_value' title='病案号、姓名、出院科室、手术名称' placeholder='病案号、姓名、出院科室、手术名称'  value='"+condition+"'></label>")
	}else{
		$("#"+tabId+" #"+tb_id+"_filter").html("<label>搜索 <input type='text' id='"+tb_id+"_filter_value' title='病案号、姓名、出院科室、手术名称' placeholder='病案号、姓名、出院科室、手术名称'></label>")
	}
 	//重写过滤方法
 	$("#"+tabId+" #"+tb_id+"_filter_value").keydown(function(e){
 		if(e.keyCode==13){
 			var condition = $("#"+tabId+" #"+tb_id+"_filter_value").val();
 			if(condition.length==0){
 				$('#'+tabId+' #caseTotalCount').val(0);//总条数赋值到隐藏域
 			}
 			loadPageData(tb_id,param,condition);
 		}
 	});
}